package com.test.mchelepesa.util_rice.analytics_rice.util_rice

import android.app.Application
import com.test.mchelepesa.util_rice.analytics_rice.event_rice.IAnalyticsEvent_rice

//import com.google.android.gms.tasks.Task
//import com.google.firebase.analytics.FirebaseAnalytics
//import com.google.firebase.analytics.ktx.analytics
//import com.google.firebase.analytics.ktx.logEvent
//import com.google.firebase.ktx.Firebase
import com.test.mchelepesa.constant_rice.Url_rice
import com.test.mchelepesa.util_rice.analytics_rice.bean_rice.ParamFirebaseS2S_rice
import com.test.mchelepesa.util_rice.analytics_rice.http_rice.HttpHelper_rice
import com.google.gson.Gson
import com.test.mchelepesa.bean_rice.StaticConfig_rice

//tag2025
object FirebaseUtil_rice {

    /** 句柄 */
//    private var mFirebaseAnalytics: FirebaseAnalytics = Firebase.analytics

    /** firebase app id, 服务端回推firebase事件使用 */
    private var mFirebaseAppId: String = ""

    /** ga4 instance id, 服务端回推firebase事件使用 */
    private var mGa4InstanceId: String = ""

    /**
     * 初始化
     *
     * @param context Application
     */
    fun init(context: Application) {
//        try {
//            if (mFirebaseAppId.isBlank()) {
//                Thread {
//                    mFirebaseAppId = FirebaseAnalytics.getInstance(context).firebaseInstanceId
//                    println("mFirebaseAppId=$mFirebaseAppId")
//                }.start()
//            }
//
//            if (mGa4InstanceId.isBlank()) {
//                val gResult: Task<String> = FirebaseAnalytics.getInstance(context).appInstanceId
//                gResult.addOnSuccessListener {
//                    mGa4InstanceId = it ?: ""
//                    println("mGa4InstanceId=$mGa4InstanceId")
//                }
//            }
//        } catch (e: Exception) {
//            StringUtil_rice.printException(e)
//        }
    }

    /**
     * 打点
     *
     * @param event 事件名
     */
    fun addEvent(event: IAnalyticsEvent_rice) {
//        try {
//            when (event) {
//                IAnalyticsEvent_rice.AT_REGISTER -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SIGN_UP) {
//                        param(FirebaseAnalytics.Param.METHOD, IAnalyticsEvent_rice.AT_REGISTER.name)
//                    }
//                }
//                IAnalyticsEvent_rice.AT_LOAN_APPLY -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE) {
//                        param(
//                            FirebaseAnalytics.Param.CONTENT_TYPE,
//                            IAnalyticsEvent_rice.AT_LOAN_APPLY.name
//                        )
//                        param(
//                            FirebaseAnalytics.Param.AFFILIATION,
//                            IAnalyticsEvent_rice.AT_LOAN_APPLY.name
//                        )
//                        param(FirebaseAnalytics.Param.COUPON, IAnalyticsEvent_rice.AT_LOAN_APPLY.name)
//                        param(
//                            FirebaseAnalytics.Param.TRANSACTION_ID,
//                            IAnalyticsEvent_rice.AT_LOAN_APPLY.name
//                        )
//                        param(FirebaseAnalytics.Param.VALUE, "1")
//                        param(FirebaseAnalytics.Param.SHIPPING, "1")
//                        param(FirebaseAnalytics.Param.TAX, "1")
//                        param(FirebaseAnalytics.Param.CURRENCY, "USD")
//                    }
//                }
//                else -> {}
//            }
//        } catch (e: Exception) {
//            StringUtil_rice.printException(e)
//        }
    }

    /**
     * 保存firebase s2s信息
     */
    fun saveS2SInfo(targetUrlList: List<String>) {
        val params = ParamFirebaseS2S_rice()
        val body = Gson().toJson(params).trim()
        val path =
            StaticConfig_rice.getBaseUrl() + StaticConfig_rice.getCountryUrlPrefix() + Url_rice.PATH_FIREBASE_S2S
        println("====== $path")

        HttpHelper_rice.postData(path, body)
    }

    /**
     * 获取Firebase App Id
     */
    fun getFirebaseAppId(): String {
        return mFirebaseAppId
    }

    /**
     * 获取Firebase Ga4 instance Id
     */
    fun getGa4InstanceId(): String {
        return mGa4InstanceId
    }
}